package com.itheima.controller;

import com.github.pagehelper.PageInfo;
import com.itheima.domain.User;
import com.itheima.results.Result;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author 王加鑫
 * @Date 2021/07/28 15:37
 * @Since JDK 1.8
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/save")
    public Result save(User user){
        System.out.println("user = " + user);
        userService.save(user);
        return Result.ofSuccess();
    }

    @PostMapping("/update")
    public Result update(User user){
        System.out.println("user = " + user);
        userService.update(user);
        return Result.ofSuccess();
    }

    @RequestMapping("/delete")
    public Result delete(Integer id){
        userService.delete(id);
        return Result.ofSuccess();
    }

    @RequestMapping("/get")
    public Result get(Integer id){
        User user = userService.get(id);
        System.out.println("id = " + id);
        System.out.println("user = " + user);
        return Result.ofSuccess(user);
    }

    @RequestMapping("/getAll")
    public Result getAll(int page,int size){
        PageInfo<User> all = userService.getAll(page, size);
        return Result.ofSuccess(all);
    }

    @RequestMapping("/login")
    public Result login(String username,String password){
        User u = userService.login(username, password);
        return Result.ofSuccess(u);
    }
}
